﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.OleDb;

public partial class admin_sysMenu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //生成树结构
    public void getTree() {
        StringBuilder html = new StringBuilder();
        html.Append("<script language=\"javascript\">\n");
        html.Append(" var tree = new MzTreeView(\"tree\");\n");
        html.Append("tree.setIconPath(\"TreeView/\");\n");
        //html.Append("tree.nodes[\"0_1\"] = \"text:系统菜单;method:sys()\";\n");

        //读取菜单

        OleDbConnection conn = DB.conn();
        if (conn != null )
        {
            conn.Open();
            OleDbCommand comm = new OleDbCommand();
            comm.Connection = conn;

           
            comm.CommandText = "select * from sys_menu  order by menuOrder";
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = comm;
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            string par = "";
            string cur = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                par="0";
				cur="";
	  			if( dt.Rows[i]["par_id"]!=null && !dt.Rows[i]["par_id"].ToString().Trim().Equals("0") ){
	  				par="0"+dt.Rows[i]["par_id"];
                }
	  			cur="0"+dt.Rows[i]["id"];
                html.Append("tree.nodes[\"" + par + "_" + cur + "\"] = \"text:" + dt.Rows[i]["menuName"].ToString() + ";method:show('" + dt.Rows[i]["id"].ToString() + "','" + dt.Rows[i]["par_id"].ToString() + "','" + dt.Rows[i]["menuName"].ToString() + "','" + dt.Rows[i]["menuUrl"].ToString() + "','" + dt.Rows[i]["menuOrder"].ToString() + "','" + dt.Rows[i]["menuShow"].ToString() + "','" + dt.Rows[i]["menuAbout"].ToString() + "')\";\n");
            }
            conn.Close();
        }

        html.Append("document.write(tree.toString());\n");
        html.Append("</script>\n");
        Response.Write(html.ToString());
    }
    //保存
    public void save(object sender, EventArgs e)
    {

        string methodStr = method.Text.Trim();
        string idStr = id.Text.Trim().Equals("") ? "0" : id.Text.Trim();
        string paridStr = parid.Text.Trim().Equals("") ? "0" : parid.Text.Trim();
        string menuNameStr = menuName.Text.Trim().Replace("'", "‘").Replace("\"", "“");
        string menuUrlStr = menuUrl.Text.Trim().Replace("'", "‘").Replace("\"", "“");
        string menuOrderStr = menuOrder.Text.Trim().Equals("") ? "0" : menuOrder.Text.Trim();
        string menuShowStr = menuShow.Text.Trim().Equals("") ? "1" : menuShow.Text.Trim();

        try
        {
            //添加
            if ("add".Equals(methodStr))
            {
                SysMenuDao.add(paridStr, menuNameStr, menuUrlStr, menuOrderStr, menuShowStr);
            }
            //更新
            else if ("update".Equals(methodStr))
            {
                SysMenuDao.update(idStr,paridStr, menuNameStr, menuUrlStr, menuOrderStr, menuShowStr);
            }
        }
        catch (Exception ex) {
            Response.Write("<script>alert('操作失败');</script>");
            Response.Write("失败原因："+ex);
        }
    }
    //保存
    public void del(object sender, EventArgs e)
    {

        string idStr = id.Text.Trim().Equals("") ? "0" : id.Text.Trim();

        try
        {
            SysMenuDao.del(idStr);
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('操作失败');</script>");
            Response.Write("失败原因：" + ex);
        }
    }
}
